/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SnpDbHandling;

import SnpDbEntities.CodeTypes;
import SnpDbEntities.Snippet;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 *
 * @author Łukasz 'byku' Guzik
 */
public class SnippetRecord
{
    private final Snippet snippet;

    public  SnippetRecord(Snippet snippet)
    {
	this.snippet = snippet;
    }

    public final Snippet getSnippet()
    {
	return snippet;
    }

    public static SnippetRecord getFromResultSet(ResultSet rs, boolean withData) throws SQLException
    {
	Snippet snippet = new Snippet();

	snippet.setId(rs.getInt("ID"));
	snippet.setName(rs.getNString("Name"));
	snippet.setDesc(rs.getNString("Description"));
	snippet.setLastModified(new Date(rs.getTimestamp("LastModified").getTime()));
	snippet.setOwnerId(rs.getInt("OwnerID"));
	snippet.setCategoryId(rs.getInt("CategoryID"));
	snippet.setCodeType(CodeTypes.valueOf(rs.getNString("CodeType")));

	if(withData)
	{
	    snippet.setData(rs.getNString("Data"));
	}

	return new SnippetRecord(snippet);
    }

    public void insertIntoResultSet(ResultSet rs, boolean withData) throws SQLException
    {
	rs.updateNString("Name", snippet.getName());

	if(snippet.getDesc() != null)
	{
	    rs.updateNString("Description", snippet.getDesc());
	}
	else
	{
	    rs.updateNull("Description");
	}
	rs.updateTimestamp("LastModified", new java.sql.Timestamp(snippet.getLastModified().getTime()));
	rs.updateInt("OwnerID", snippet.getOwnerId());
	rs.updateInt("CategoryID", snippet.getCategoryId());
	rs.updateNString("CodeType", snippet.getCodeType().toString());

	if(withData)
	{
	    rs.updateNString("Data", snippet.getData());
	}
    }
}
